﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.User.CurrentProfile
{
    using SharingCenter.Web.Classes;
    using System.Collections.Specialized;
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes.Profiles;
    using SharingCenter.Web.Shared;

    public partial class Default : SharingCenter.Web.Classes.PageBase
    {
        protected Sc_Profile _myProfile = null;
        PrivacyDropDownList pddName,
            pddGender,
            pddBirthDate,
            pddLocation,
            pddInterest,
            pddSpeciality,
            pddUrl;
        RadioButtonList rdoGender;
        MultiView mvGender;

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                //dataContext.Refresh(System.Data.Linq.RefreshMode.KeepChanges, dataContext.Sc_Profiles);
                _myProfile = ProfileOperations.GetProfile(Profile.UserId);
                //dataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, _myProfile);
            }
            catch { RedirectToDefaultPage(); }

            FormView1.ItemUpdating += new FormViewUpdateEventHandler(FormView1_ItemUpdating);
            FormView1.ItemUpdated += new FormViewUpdatedEventHandler(FormView1_ItemUpdated);
            FormView1.DataBound += new EventHandler(FormView1_DataBound);
            LinqDataSource1.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSource1_Selecting);
            LinqDataSource1.Updated += new EventHandler<LinqDataSourceStatusEventArgs>(LinqDataSource1_Updated);
        }

        protected void Page_PreLoad(object sender, EventArgs e)
        {
            
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            FindPrivacyControls();

            if (!IsPostBack)
            {
                LoadPrivacySettings();
                mvGender.ActiveViewIndex = _myProfile.Gender == SharingCenter.Web.Classes.enums.Gender.NA ? 0 : 1;
            }
            
        }

        void LinqDataSource1_Updated(object sender, LinqDataSourceStatusEventArgs e)
        {
            //throw new NotImplementedException();
        }

        void FindPrivacyControls()
        {
            pddName = (PrivacyDropDownList)FormView1.FindControl("pddName");
            pddGender = (PrivacyDropDownList)FormView1.FindControl("pddGender");
            pddBirthDate = (PrivacyDropDownList)FormView1.FindControl("pddBirthDate");
            pddLocation = (PrivacyDropDownList)FormView1.FindControl("pddLocation");
            pddInterest = (PrivacyDropDownList)FormView1.FindControl("pddInterest");
            pddSpeciality = (PrivacyDropDownList)FormView1.FindControl("pddSpeciality");
            pddUrl = (PrivacyDropDownList)FormView1.FindControl("pddUrl");
            rdoGender = (RadioButtonList)FormView1.FindControl("rdoGender");
            //rdoGender.DataBinding += new EventHandler(rdoGender_DataBinding);
            mvGender = (MultiView)FormView1.FindControl("mvGender");
        }

        //void rdoGender_DataBinding(object sender, EventArgs e)
        //{
        //    if (_myProfile.Gender != null)
        //    {
        //        rdoGender.Items.FindByValue(_myProfile.Gender.ToString()).Selected = true;
        //    }
        //}

        void LoadPrivacySettings()
        {
            try
            {
                pddName.NamedPrivacy = Profile.PrivacyPersonal.FullName;
            }
            catch { }

            try
            {
                pddGender.NamedPrivacy = Profile.PrivacyPersonal.Gender;
            }
            catch { }

            try
            {
                pddBirthDate.NamedPrivacy = Profile.PrivacyPersonal.BirthDate;
            }
            catch { }
            try
            {
                pddLocation.NamedPrivacy = Profile.PrivacyPersonal.Location;
            }
            catch { }
            try
            {
                pddInterest.NamedPrivacy = Profile.PrivacyPersonal.Interest;
            }
            catch { }
            try
            {
                pddSpeciality.NamedPrivacy = Profile.PrivacyPersonal.Speciality;
            }
            catch { }
            try
            {
                pddUrl.NamedPrivacy = Profile.PrivacyPersonal.Url;
            }
            catch { }
        }

        void SavePrivacySettings()
        {
            Profile.PrivacyPersonal.FullName = pddName.NamedPrivacy;
            Profile.PrivacyPersonal.Gender = pddGender.NamedPrivacy;
            Profile.PrivacyPersonal.BirthDate = pddBirthDate.NamedPrivacy;
            Profile.PrivacyPersonal.Location = pddLocation.NamedPrivacy;
            Profile.PrivacyPersonal.Interest = pddInterest.NamedPrivacy;
            Profile.PrivacyPersonal.Speciality = pddSpeciality.NamedPrivacy;
            Profile.PrivacyPersonal.Url = pddUrl.NamedPrivacy;
            //Profile.Save();
        }

        void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            e.Result = _myProfile;
        }

        void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
        {
            
        }

        void FormView1_DataBound(object sender, EventArgs e)
        {
            if (FormView1.DataItem != null)
            {
                PopulateCountryProvinceValues();
            }
        }

        void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            Profile.Website.ProfileUpdated = true;
            Profile.Save();
            //scDataContext.Instance.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, _myProfile);
            UpdateProfile(e.NewValues);
            SetTopMostMessage("个人信息已保存。", true);
            RefreshPage();
        }

        void UpdateProfile(IOrderedDictionary newValues)
        {
            Profile.Personal.NickName = newValues["NickName"].ToString();
            Profile.Save();
        }

        void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            LinqDataSource1.Select = null;
            SavePrivacySettings();

            if (_myProfile.Gender == SharingCenter.Web.Classes.enums.Gender.NA)
            {
                e.NewValues["Gender"] = (Gender)int.Parse(rdoGender.SelectedValue);
            }

            if (e.NewValues["BirthDate"].ToString() == "")
            {
                e.NewValues["BirthDate"] = null;
            }

            string selectedCountry = ((DropDownList)FormView1.FindControl("DropDownList1")).SelectedValue;

            if (selectedCountry == "")
            {
                e.NewValues["Country"] = ((TextBox)FormView1.FindControl("txtCountryName")).Text.Trim();
            }
            else if("中国" == selectedCountry)
            {
                e.NewValues["Country"] = selectedCountry;

                ProvinceCityDropDownList lst = (ProvinceCityDropDownList)FormView1.FindControl("ProvinceCityDropDownList1");
                    e.NewValues["Province"] = lst.SelectedProvince;
                    e.NewValues["City"] = lst.SelectedCity;
            }
        }

        protected void RadioButtonList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }        

        void PopulateCountryProvinceValues()
        {
            DropDownList countryDropDownList = ((DropDownList)FormView1.FindControl("DropDownList1"));
            DropDownList provinceDropDownList = ((DropDownList)FormView1.FindControl("ddProvince"));
            //UserProfile p = (UserProfile)FormView1.DataItem;
            TextBox countryName = (TextBox)FormView1.FindControl("txtCountryName");
            TextBox provinceName = (TextBox)FormView1.FindControl("txtProvinceName");

            if (_myProfile!=null && _myProfile.Country == null)
            {
                countryDropDownList.Items.FindByValue("中国").Selected = true;
            }
            else
            {
                try
                {
                    countryDropDownList.Items.FindByValue(_myProfile.Country).Selected = true;
                }
                catch { countryDropDownList.Items.FindByValue("").Selected = true; }
                
                if (_myProfile.Country == "中国")
                {
                    //provinceName.Attributes.CssStyle.Add("display", "none");
                    countryName.Attributes.CssStyle.Add("display", "none");
                }
                else
                {
                    //provinceDropDownList.Attributes.CssStyle.Add("display", "none");   
                }
            }
        }
    }
}
